1   /*
2    * Copyright (C) 2011 The Guava Authors
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
18  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
19  
20  import com.google.common.annotations.GwtCompatible;
21  import com.google.common.collect.testing.IteratorFeature;
22  import com.google.common.collect.testing.IteratorTester;
23  import com.google.common.collect.testing.features.CollectionFeature;
24  
25  import java.util.Arrays;
26  import java.util.Iterator;
27  
28  /**
29   * Tester to make sure the {@code iterator().remove()} implementation of {@code Multiset} works when
30   * there are multiple occurrences of elements.
31   * 
32   * @author Louis Wasserman
33   */
34  @GwtCompatible(emulated = true)
35  public class MultisetIteratorTester<E> extends AbstractMultisetTester<E> {
36    @SuppressWarnings("unchecked")
37    @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER})
38    public void testRemovingIteratorKnownOrder() {
39      new IteratorTester<E>(4, IteratorFeature.MODIFIABLE, getSubjectGenerator().order(
40          Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
41          IteratorTester.KnownOrder.KNOWN_ORDER) {
42        @Override
43        protected Iterator<E> newTargetIterator() {
44          return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
45              .iterator();
46        }
47      }.test();
48    }
49  
50    @SuppressWarnings("unchecked")
51    @CollectionFeature.Require(value = SUPPORTS_ITERATOR_REMOVE, absent = KNOWN_ORDER)
52    public void testRemovingIteratorUnknownOrder() {
53      new IteratorTester<E>(4, IteratorFeature.MODIFIABLE, Arrays.asList(samples.e0, samples.e1,
54          samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
55        @Override
56        protected Iterator<E> newTargetIterator() {
57          return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
58              .iterator();
59        }
60      }.test();
61    }
62  
63    @SuppressWarnings("unchecked")
64    @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
65    public void testIteratorKnownOrder() {
66      new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, getSubjectGenerator().order(
67          Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
68          IteratorTester.KnownOrder.KNOWN_ORDER) {
69        @Override
70        protected Iterator<E> newTargetIterator() {
71          return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
72              .iterator();
73        }
74      }.test();
75    }
76  
77    @SuppressWarnings("unchecked")
78    @CollectionFeature.Require(absent = {SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER})
79    public void testIteratorUnknownOrder() {
80      new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, Arrays.asList(samples.e0, samples.e1,
81          samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
82        @Override
83        protected Iterator<E> newTargetIterator() {
84          return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
85              .iterator();
86        }
87      }.test();
88    }
89  }
90